package triangle.freshmanemail;

import java.util.ArrayList;

import triangle.utils.SaveLoad;

public class EmailCSVReader {

	private static final int LASTNAMEIDX = 0;
	private static final int FIRSTNAMEIDX = 1;
	private static final int MIDDLENAMEIDX = 2;
	private static final int MINLEN = 3; // lazy hack
	
	public static ArrayList<EmailStoreStructure> getEmailStructures(String filename)
	{
		ArrayList<EmailStoreStructure> ess = new ArrayList<EmailStoreStructure>();
		String fileContent = SaveLoad.loadFile(filename);
		String [] lines = fileContent.split("\n");
		for (String line : lines)
		{
			ess.add(getEmailStoreStructureFromLine(line));
		}
		return ess;
	}
	/**
	 * From one line of CSV, returns first name, last name, middle name and email
	 * @param line
	 * @return
	 */
	private static EmailStoreStructure getEmailStoreStructureFromLine(String line)
	{
		String [] sepLine = line.split(",");
		// doesn't store emails yet
		if (sepLine.length >= MINLEN)
			return new EmailStoreStructure(sepLine[FIRSTNAMEIDX], sepLine[LASTNAMEIDX], sepLine[MIDDLENAMEIDX]); 
		else return new EmailStoreStructure("", "", "");
	}
}
